﻿@model WidgetTemplate
@{
    ViewBag.Title = "WidgetEditor";
    Layout = "~/Views/Shared/_Dashboard.cshtml";
    var installedPath = Request.QueryString["path"];
    var widgetImage = "~/content/images/icon_puzzle_48.png";
    if (Model.Icons != null && Model.Icons.Count > 0)
    {
        foreach (var icon in Model.Icons)
        {
            if ((icon.Width == 48) && (icon.Height == 48) && !string.IsNullOrEmpty(icon.Source))
            {
                widgetImage = "~/content/widgets/" + installedPath + "/" + Model.Name + "/" + icon.Source;
            }
        }
    }
    var userprefArgs = "[]";
    if ((Model.UserPreferences != null) && (Model.UserPreferences.Count > 0))
    {
        var userPrefs=(from u in Model.UserPreferences
                      select new {
                          paramName=u.Name,
                          paramType=u.Type,
                          defaultValue=u.Value}).ToArray();
        userprefArgs =Json.Encode(userPrefs);
    }

}
@UIHelper.ImageTitle(!string.IsNullOrEmpty(Model.Defaults.Title) ? Model.Defaults.Title : Model.Name, Model.Description, widgetImage)
@UIHelper.SectionHeader(Model.Content.ContentType)
@using (Html.BeginForm())
{
    <table>
        <tr>
            <td style="vertical-align:top;">
                @{Ajax.Dna().Tabs("codeTab")
                      .Views(views =>
                      {
                          views.Add(Html.Global("RES_CODE")).Template(@<text>
                    @if (Model.Content.ContentType.Equals("html", StringComparison.OrdinalIgnoreCase))
                    {
                        Ajax.Dna().RichTextBox("ContentHtml").Value(Model.Content.Text).Resizable().Width(700).Height(400).Render();
                    }
                    else
                    {
                        <div>@Ajax.Dna().TextArea("ContentHtml").Value(Model.Content.Text).Width(600).Height(400).Resizable().GetHtml()</div>
                    }
                    </text>);
                      })
                      .Render();}
            </td>
            <td style="width: 310px;vertical-align:top;">
                <div class="d-widget-zone" id="previewZone" style="margin: 0px 0px 5px 0px; padding: 0px;">
                </div>
                @{ 
                  Ajax.Dna().CollapsablePanel()
                    .HtmlAttributes(new { style = "margin-bottom:5px;" })
     .Title(Html.Global("RES_USER_PREFERENCES"))
     .Body(@<text>
                    <table id="uf_table" style="empty-cells: show;">
                        <tr>
                            <td>@Html.Global("RES_NAME")
                            </td>
                            <td>@Html.Global("RES_TYPE")
                            </td>
                            <td>@Html.Global("RES_DEFAULT_VALUE")
                            </td>
                            <td>
                            </td>
                        </tr>
                        <tr>
                            <td>@Ajax.Dna().TextBox("uf_name").Width(80).GetHtml()
                            </td>
                            <td>@(Ajax.Dna().ComboBox("uf_types").Width(100)
                            .Items(items =>
                            {
                                items.AddValue("Text", "System.String");
                                items.AddValue("Integer", "System.Int32");
                                items.AddValue("Decimal", "System.Decimal");
                                items.AddValue("Boolean", "System.Boolean");
                            }).GetHtml())
                            </td>
                            <td>@Ajax.Dna().TextBox("uf_def").Width(60).GetHtml()
                            </td>
                            <td>@Ajax.Dna().Button("uf_add").ChangeType(ButtonTypes.ImageButton).Images(Url.Content("~/content/images/add.png")).Click("_addUserPreference();").GetHtml()
                            </td>
                        </tr>
                    </table>
                    </text>).Render();
                }
            </td>
        </tr>
    </table>
    <div>
        @Ajax.Dna().Button().LocText("RES_SAVE").Submit().GetHtml()
    </div>
}
@Html.Partial("WidgetClientTemplate")
<script type="text/x-jquery-tmpl" id="uf_template">
<tr><td>${paramName}</td><td>${paramType}</td><td>${defaultValue}</td><td><img src="@Url.Content("~/content/images/icon_delete_16.png")" alt="del" onclick="_removeUserPreference(this);" /></td></tr>
</script>
<script type="text/javascript">
    $(function(){
          _preview();
          var userPreferences=@MvcHtmlString.Create(userprefArgs);
          $.each(userPreferences,function(i,n){ 
             $("#uf_template").tmpl(n).appendTo($("#uf_table"));
          });
   });

    function _preview() {
        $.ajax({
        url:"@(Url.Action("LoadDescriptor", "Widget"))?wid=@(Server.UrlEncode(Model.ID))",
        type:"json",
        success:function(dat){
                dat.IsExpanded=true;
                if (dat.ID=="0")
                  dat.ID="@(Server.UrlEncode(Model.ID))";
                $("#widget_template").tmpl(dat).appendTo($("#previewZone"))
                                                          .portlet({
                                                              preview: true,
                                                              design: true,
                                                              baseUrl: "@Url.Content("~/Widget/")"
                                                          });
        }
        });
    }

    function _removeUserPreference(element) {
      var _item=  $(element).parent().parent();
      var dat=_item.tmplItem().data;
      $.extend(dat ,{wid:"@(Server.UrlEncode(Model.ID))"});
        $.ajax({
           url:"@Url.Action("DeleteParam", "Widget")",
           data:dat,
           success:function(dat) {
           if (dat){
              _item.remove();
           }
          }
        });
    }

    function _addUserPreference() {
        var _ufParam = {
            paramName: $("#uf_name").val(),
            paramType: $("#uf_types").val(),
            defaultValue: $("#uf_def").val(),
            wid:"@(Server.UrlEncode(Model.ID))"
        };

        if (_ufParam.paramName == "") {
            $("#uf_name").focus();
            return;
        }

        if (_ufParam.pararmType == "") {
            $("#uf_types").focus();
            return;
        }
        $.ajax({
           url:"@Url.Action("AddParam", "Widget")",
           data:_ufParam,
           success:function(dat) {
           if (dat){
              $("#uf_template").tmpl(_ufParam).appendTo($("#uf_table"));
              $("#uf_name").val("");
              $("#uf_def").val("");
            }
          }
        });
    }
</script>
